package org.youroute.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public abstract class BBDD {
	//private final static String URL   = "jdbc:mysql://localhost/youroute";
	private final static String URL   = "jdbc:mysql://10.10.19.191/youroute";
	private final static String DRIVER="com.mysql.jdbc.Driver";
	private final static String USER="youroute";
	private final static String PASS="youroute";
	private static Connection con=null;
	private static Statement stm=null;
	
	private static Connection getConexion(){
		try {
			if ((con==null)||(con.isClosed())){
				Class.forName  (DRIVER);
	      		con = DriverManager.getConnection 
			  ( URL, USER, PASS );
			}
			return con;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}
	
	private static Statement getStatement(){
		if (stm==null){
			try{
				stm = getConexion().createStatement();
			} catch (SQLException e) {
				return null;
			}
		}
		return stm;
	}
	
	public static ResultSet ejecutarConsulta(String sql){
		try {
			return getStatement().executeQuery(sql);
		} catch (SQLException e) {
			System.out.println(e);
			return null;
		}
	}

	
	
	public static int ejecutarActualizacion(String sql){
		try {
			return getStatement().executeUpdate(sql);
		} catch (SQLException e) {
			return -1;
		}
	}
	
}
